<template>
  <div class="announcement-detail-page">
    <!-- 顶部导航栏 -->
    <Navbar />

    <div class="container" v-loading="loading">
      <!-- 面包屑导航 -->
      <el-breadcrumb separator="/" class="breadcrumb">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/announcement' }">公告中心</el-breadcrumb-item>
        <el-breadcrumb-item>公告详情</el-breadcrumb-item>
      </el-breadcrumb>

      <div v-if="!loading && announcement" class="detail-content">
        <!-- 公告头部 -->
        <div class="announcement-header">
          <div class="header-tags">
            <el-tag v-if="announcement.isTop === '1'" type="danger" size="large" effect="dark">
              <el-icon><Top /></el-icon>
              置顶
            </el-tag>
            <el-tag :type="getTypeTag(announcement.announcementType)" size="large">
              {{ getTypeName(announcement.announcementType) }}
            </el-tag>
          </div>
          
          <h1 class="announcement-title">{{ announcement.title }}</h1>
          
          <div class="announcement-meta">
            <span class="meta-item">
              <el-icon><User /></el-icon>
              发布者：{{ announcement.publisherName }}
            </span>
            <span class="meta-item">
              <el-icon><Clock /></el-icon>
              发布时间：{{ announcement.publishTime }}
            </span>
            <span class="meta-item">
              <el-icon><View /></el-icon>
              浏览：{{ announcement.viewCount }} 次
            </span>
          </div>
        </div>

        <el-divider />

        <!-- 公告内容 -->
        <div class="announcement-content">
          <div class="content-text" v-html="formatContent(announcement.content)"></div>
        </div>

        <!-- 底部操作 -->
        <div class="announcement-footer">
          <el-button @click="goBack">
            <el-icon><ArrowLeft /></el-icon>
            返回列表
          </el-button>
        </div>
      </div>

      <el-empty v-if="!loading && !announcement" description="公告不存在" />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { getAnnouncementDetail } from '@/api/announcement'
import { User, Clock, View, Top, ArrowLeft } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import Navbar from '@/components/Navbar.vue'

const route = useRoute()
const router = useRouter()

const announcement = ref(null)
const loading = ref(false)

// 获取公告详情
const loadAnnouncementDetail = async () => {
  const announcementId = route.params.announcementId
  if (!announcementId) return

  loading.value = true
  try {
    const data = await getAnnouncementDetail(parseInt(announcementId))
    announcement.value = data
  } catch (error) {
    console.error('获取公告详情失败:', error)
    ElMessage.error('获取公告详情失败')
  } finally {
    loading.value = false
  }
}

// 获取类型名称
const getTypeName = (type) => {
  const typeMap = {
    '0': '系统公告',
    '1': '活动公告',
    '2': '重要通知'
  }
  return typeMap[type] || '未知'
}

// 获取类型标签颜色
const getTypeTag = (type) => {
  const typeMap = {
    '0': 'info',
    '1': 'success',
    '2': 'warning'
  }
  return typeMap[type] || 'info'
}

// 格式化内容（将换行符转为 <br>）
const formatContent = (content) => {
  if (!content) return ''
  return content.replace(/\n/g, '<br>')
}

// 返回列表
const goBack = () => {
  router.push('/announcement')
}

onMounted(() => {
  loadAnnouncementDetail()
})
</script>

<style scoped>
.announcement-detail-page {
  width: 100%;
  min-height: 100vh;
  background-color: #f5f5f5;
  margin: 0;
  padding: 0;
}

.container {
  max-width: 1000px;
  margin: 0 auto;
  padding: 20px;
}

.breadcrumb {
  margin-bottom: 20px;
  padding-top: 20px;
}

.detail-content {
  background: white;
  border-radius: 8px;
  padding: 40px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.announcement-header {
  margin-bottom: 30px;
}

.header-tags {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

.announcement-title {
  font-size: 32px;
  font-weight: bold;
  color: #333;
  margin: 0 0 20px 0;
  line-height: 1.4;
}

.announcement-meta {
  display: flex;
  gap: 24px;
  flex-wrap: wrap;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  color: #666;
}

.meta-item .el-icon {
  font-size: 16px;
  color: #999;
}

.announcement-content {
  margin: 30px 0;
}

.content-text {
  font-size: 16px;
  line-height: 2;
  color: #333;
  text-align: justify;
  word-wrap: break-word;
}

.content-text :deep(p) {
  margin-bottom: 16px;
}

.announcement-footer {
  margin-top: 40px;
  padding-top: 20px;
  border-top: 1px solid #e8e8e8;
  display: flex;
  justify-content: center;
}

@media (max-width: 768px) {
  .detail-content {
    padding: 20px;
  }

  .announcement-title {
    font-size: 24px;
  }

  .content-text {
    font-size: 15px;
  }

  .announcement-meta {
    flex-direction: column;
    gap: 12px;
  }
}
</style>
